In this lecture, we're going to be using the badge service to award a badge to our player.
When they win the game.
This badge will be made permanently available within the account of the player who earns it.
So the plan is to award every player that is alive on the map when the final wave is beaten, and give
them a badge.
In order to add a new badge to our game, we can head to the Creator dashboard on Roblox from our home
page, and we can hit create.
And now we are in the Creator Hub and we can go ahead and find our creations underneath the creations
tab.
Or I can find mine right here, which is my example wave game.
So I'll select my game here, and then we can head over to the engagement section for our game.
And down in this section right here you can find badges.
And right now I don't have any in this experience.
But we can go ahead and create a new badge.
And here we can give it a name, a description and then an image for the badge.
So I'm just going to call this badge you survived and give it some description like you successfully.
Beat all the waves in the game.
Something like that.
And then you can also go ahead and upload an image for the badge as well, which I'm going to do.
And once you've got your image uploaded for your badge, you can go ahead and hit the create badge function.
And it also warns us that the first five badge creations are free.
And then you have to pay 100 Robux for any additional badges.
So we'll go ahead and create this badge.
There we go.
We have the you survived badge.
And the important thing we're going to need from this badge is its ID.
So we can hit these three dots and copy the asset ID of this badge.
Now back inside of studio, we're going to need this badge in order to award it to our player.
Now I want to go ahead and store any badges in my game inside of a module script that's going to be
acting as an enum.
That way we have a standard and know exactly what ID belongs to what, so we could correlate the id
we just copied with the victory badge.
So what we can go ahead and do is inside of replicated storage.
Inside of the modules folder we'll go and create a new module script.
And I'm going to call this my badge enum.
And we're simply just going to return a table that's going to contain the different badge IDs.
We would like to be able to reward to our players.
I'm going to call this one my Victory Badge.
And I'm going to set it equal to a table.
And this table is going to contain the ID of the badge.
So I'll just paste my ID right there for the badge.
And then I can also enter in the name for my badge as well.
So the name of my badge I'll just say victory badge.
And of course we can use this name for the future.
For example, if you wanted to give any players like special benefits in the game when they have a particular
badge, you can check if they have this badge, and then you could apply an attribute on them with this
name.
And then from that point, any script could check if a player has this attribute on them and then give
them some kind of special benefit.
And then we can go back to our game handler server script and go ahead and grab the badge service in
order to award a badge to our player.
And then we can also get our badge enum.
Will require for replicated storage modules.
Enums.
Dot badge enum.
And then I'm going to create a dedicated function for awarding a badge to a player.
I'll call this function award badge and we'll pass the ID of the player.
So we'll call this player ID.
And then we also need the badge ID we want to award to this particular player.
Then inside of the badge service we have a function called award badge.
And it says Award a badge to a player given the ID of each.
So we pass the user ID of the player and then the badge ID.
So we'll pass our player ID and then we'll pass our badge ID.
And then I'm going to wrap this function in a p call, just in case this award badge function encounters
a problem.
So I'm going to call this success results is equal to p call.
We're going to paste that in there and return the result from our award badge function.
And then we can check to see if we were successful in awarding a badge to this player.
If we were not successful, then we could just print something in the console like failed to award badge,
and then we could give the reason why.
And I'll print that on a new line with backslash n, and then we can concatenate it with the results
from our p call.
And now what we can go ahead and do is use this function to award a badge to all of the players that
have beaten the final wave, and they're still alive.
So we can go ahead and scroll down.
And when the function for the game over executes for when the players win, well, we can go ahead and
do is after we play the victory sound, we'll loop through every single player that's on the alive team.
So in Teams Alive, get players and we'll award a badge to all these players.
So we'll pass the player dot user ID and then we'll also pass the badge ID we would like to award to
the player, which would be inside of our badge enum.
And that's going to be the victory badge.
And we're going to get that ID now in order to test this, what I'm going to do is I'm going to decrease.
My total waves amount to one.
That way I don't have to sit through 11 waves.
So once we beat the first wave, then this loop's going to think that all of the players have won,
and then it should award us the badge that, you know, we've won the game.
So let's go ahead and play test this game.
The waves starting.
So let's go ahead and kill all of these zombies off.
All right, here's my last three zombies.
And if I kill all of these guys, hopefully I get awarded that badge.
So when I kill this guy, we should win.
Let's see what happens.
Oh!
Game over.
We won.
And there we go.
Badge awarded.
We got the you Survived award.
And now that badge has been added to our account.
Very cool.
That's all I have for you in this lecture, and I'll see you in the next one.